package com.zuul.main.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @create 2020-11-21 22:50
 */
@Configuration
public class CrossOriginConfig {
    @Bean
    public CorsFilter corsFilter() {
        //创建跨域配置源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //创建跨域配置类
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许所有的请求有
        corsConfiguration.addAllowedHeader("*");
        //允许所有的的服务器请求
        corsConfiguration.addAllowedOrigin("*");
        //允许所有的请求方法
        corsConfiguration.addAllowedMethod("*");

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
